#include "TcpServer.hpp"
#include "Protocol.hpp"
#include "Calculator.hpp"
#include <memory>
using namespace std;
void DefaultHandler(std::weak_ptr<Connection> conn)
{
    if(conn.expired()) return;
    auto connection = conn.lock();
    if(connection->_tcpserver.expired()) return;
    auto tcpserver = connection->_tcpserver.lock();
    //处理多个请求
    while(true)
    {
        // cout << "inbuffer: " << connection->_inbuffer << endl;
        std::string response = tcpserver->_handler(connection->_inbuffer);
        if(response.empty()) break;
        connection->_outbuffer += response;
        tcpserver->Sender(conn);
    }
}
int main()
{
    CalculatorComputer cal;
    shared_ptr<TcpServer> psvr(new TcpServer(8888, DefaultHandler, \
        std::bind(&CalculatorComputer::Calculator, &cal, std::placeholders::_1)));
    psvr->Init();
    psvr->Loop();
    return 0;
}